package org.jfk.core.utils;

import java.security.MessageDigest;

import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.authc.credential.PasswordService;

public class SecurityUtil {
	
	public static String toMD5(String str) {
		if (str == null){
			return null;
		}
		return toMD5(str.getBytes());
	}
	
	//把字节数组转换成md5
	public static String toMD5(byte[] input) {
		String md5str = null;
		try {
			//创建一个提供信息摘要算法的对象，初始化为md5算法对象
			MessageDigest md = MessageDigest.getInstance("MD5");
			//计算后获得字节数组
			byte[] buff = md.digest(input);
			//把数组每一字节换成16进制连成md5字符串
			md5str = ByteUtils.byteToHexString(buff).toLowerCase();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return md5str;
	}
	
	public static void main(String[] args){
		String password = "111111";
		String md5 = toMD5(password.getBytes());
		System.out.println(md5);
		
		PasswordService svc = new DefaultPasswordService();  
		String svcStr = svc.encryptPassword(password);
		System.out.println(svcStr);
	}
}
